<?php
// +----------------------------------------------------------------------
// |MCPHP[基于ThinkPHP6.1开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2023 http://www.mingchangruanjian.com
// +----------------------------------------------------------------------
// | Author jokerfei [ jokerfei377@gmail.com ]
// +----------------------------------------------------------------------
// | 铭畅软件 技术部 出品
// +----------------------------------------------------------------------
namespace addons\WeChat\controller;

require_once __DIR__ . '/../sdk/include.php';
/**
 * Class AuthCode 公众号授权相关基类
 * @package addons\WeChat\controller
 * @Author jokerfei [ jokerfei377@gmail.com ]
 * @created 2019/9/9 22:09
 */
class AuthCode extends Base
{

    public $mini_config;
    public $code_config;

    //返回实例本身
    function __construct($config = null)
    {
        parent::__construct();
        if (is_array($config)) {
            $this->config = array_merge($this->config, $config);
        }

        $this->mini_config = [
            'appid' => $this->config['mini_appid'],
            'appsecret' => $this->config['mini_appsecret'],
        ];
        $this->code_config = [
            'appid' => $this->config['code_appid'],
            'appsecret' => $this->config['code_appsecret'],
        ];
        return $this;
    }

    //发送公众号客服消息
    public function sendServiceMsg($openId, $content)
    {
        // 实例SDK
        $code_config = $this->code_config;
        $config = ['appid' => $code_config['appid'], 'appsecret' => $code_config['appsecret']];
        $BasicWeChat = new \WeChat\Contracts\BasicWeChat($config);
        $accessToken = $BasicWeChat->getAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$accessToken}";
        $data = [
            'touser' => $openId,
            'msgtype' => 'text',
            'text' => ['content' => $content]
        ];
        return $this->curl($url, $data);
    }

    /**
     * 小程序登录凭证校验
     * @param  [type] $code [description]
     * @return [type]       [description]
     */
    public function code2Session($code){
        // 实例SDK
        $mini = new \WeMini\Crypt($this->mini_config);
        //解码，获取用户信息
        $data = $mini->session($code);
        return $data;
    }

    /**
     * 小程序通过授权码换取手机号
     * @param  [type] $code [description]
     * @return [type]       [description]
     */
    public function getPhoneNumber($code){
        // 实例SDK
        $mini = new \WeMini\Crypt($this->mini_config);
        //解码，获取用户手机号
        $data = $mini->getPhoneNumber($code);
        return $data;
    }

    //公众号openid-获取用户信息，含unionid
    public function getUserInfo($openid)
    {
        $User = new \WeChat\User($this->code_config);
        return $User->getUserInfo($openid);
    }

    /**
     * 获取微信小程序二维码
     * @Author jokerfei [ jokerfei377@gmail.com ]
     * @created 2020/2/23 18:03
     */
    public function getUnlimitedQRCode($user_id, $path, $width = 430, $auto_color = false, $line_color = ["r" => "0", "g" => "0", "b" => "0"], $is_hyaline = true){
        // 实例SDK
        $mini = new \WeMini\Qrcode($this->mini_config);
        $path_url = $path . '?user_id='.$user_id;
        try {
            $data = $mini->createMiniScene($path_url, $width, $auto_color , $line_color, $is_hyaline);

            $img_dir = ROOT_PATH.'/storage/qrcode/'.date('Y-m-d').'/';

            \service\File::mk_dir($img_dir);
            $img_name = 'qrcode_'.$user_id.'.png';

            file_put_contents($img_dir.'/'.$img_name,$data);

            return sysconfig('site', 'site_domain').'/storage/qrcode/'.date('Y-m-d').'/'.$img_name;
        } catch (Exception $e) {
            return false;
        }
    }
    
    /**
     * 获取微信小程序临时二维码
     * @author jokerfei<215628355@qq.com>
     * @created 2020/8/20 18:03
     * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.getUnlimited.html
     */
    
    public function getQrcodeLimit($scene, $page, $width = 430, $auto_color = false, $line_color = ["r" => "0", "g" => "0", "b" => "0"], $is_hyaline = true){
        // 实例SDK
        $mini = new \WeMini\Qrcode($this->mini_config);    
        $data = $mini->createMiniScene($scene, $page, $width, $auto_color , $line_color, $is_hyaline);     
        //可以自行拼接文件头，这里返回不要改
        //data:image/jpeg;base64,
        return  base64_encode($data);    
    }

    //获取全局access_token
    public function getAccessToken() {
        // 实例SDK
        $code_config = $this->code_config;
        $config = ['appid' => $code_config['appid'], 'appsecret' => $code_config['appsecret']];
      
        $BasicWeChat = new \WeChat\Contracts\BasicWeChat($config);
        return $BasicWeChat->getAccessToken();
    }

    //创建带参二维码
    public function createTicketQrcode($scene_str)
    {
        // 实例SDK
        $code_config = $this->code_config;
        $config = ['appid' => $code_config['appid'], 'appsecret' => $code_config['appsecret']];
        $BasicWeChat = new \WeChat\Contracts\BasicWeChat($config);
        $accessToken = $BasicWeChat->getAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token={$accessToken}";
        $data = [
            'action_name' => 'QR_LIMIT_STR_SCENE',
            'action_info' => ['scene' => ['scene_str' => $scene_str]],
        ];
        return $this->curl($url, $data);
    }

    /** 
     * @Author: Bryant 
     * @Date: 2023-05-25 09:55:06 
     * @method post 
     * @title 获取JsApi使用签名
     */
    public function  getTicket($data)
    {
        $wechat = new \WeChat\Script($this->code_config);
        $res = $wechat->getJsSign($data['url']);
        return $res;
    }

}
